public class BlockingQueue {
    private int []array = new int[5];
    private volatile int size  = 0;//任务长度
    private volatile int start = 0;
    private volatile int tail = 0;

    public void put(int value) throws InterruptedException {
        synchronized (this) {
            while(size == array.length) {
                wait();
            }
            array[tail] = value;
            tail = (tail + 1) % array.length;
            size++;
            notifyAll();
        }
    }

    public synchronized int getSize() {  //synchronized   在  int  前
        return size;
    }

    public int take () throws InterruptedException {//删除头部数据
        int tmp  = 0;
        synchronized (this) {
            while(size == 0) {
                wait();
            }
            tmp = array[start];
            start = (start+1)% array.length;
            size--;
            notifyAll();
        }
        return tmp;
    }

    public static void main(String[] args) throws InterruptedException {
        BlockingQueue blockingQueue = new BlockingQueue();
        blockingQueue.put(1);
        blockingQueue.put(1);
        blockingQueue.put(1);
        blockingQueue.put(1);
        blockingQueue.put(1);
        blockingQueue.take();
        blockingQueue.put(1);
        blockingQueue.take();
        System.out.println(blockingQueue.getSize());
    }
}
